<?php
/**
 * Created by PhpStorm.
 * User: clcw908
 * Date: 2016/10/25
 * Time: 13:43
 */
namespace AuctionBack\Controller;

use AuctionCore\SceneActivity\SceneActivity;
use AuctionCore\Common\Page;

/**
 *  场次活动控制器
 *
 * Class SceneActivityController
 * @package AuctionBack\Controller
 */
class SceneActivityController extends BaseController {

	private $emp_id = 0;

	/**
	 * SceneActivityController constructor.
	 */
	public function __construct(){
		parent::__construct();
		$this->emp_id = $this->user_info['emp_id'];
	}

	/**
	 * 场次活动列表
	 */
	public function activity_list(){
		$p = I('request.p/d', 1);
		$p = $p < 2 ? 1 : $p;
		$limit = 10;
		$params = [];
		$params['branch_id'] = I('request.branch_id/d');
		$params['starttime'] = I('request.starttime','');
		$params['endtime'] = I('request.endtime','');
		$params['type'] = I('request.type/d');

		$obj = new SceneActivity();
		$arr = $obj->getSceneActivityList($p,$limit,$params);
		//分公司列表
		$bcList = $this->get_branch_company_list();
		$total = $arr['total'];
		$page = new Page($total, $limit, $params);
		$page_data = $page->page_data();
		$arr =[
			'list' => $arr['list'],
			'bc_list' => $bcList,
		];
		$this->assign('params', $params);
		$this->assign('page_data', $page_data);
		$this->assign('p',$p);
		$this->assign('arr',$arr);
		$this->display();
	}

	/**
	 * 活动详情
	 */
	public function activity_detail(){
		$activityId = I('request.activity_id/d');
		$act = I('request.act');
		$obj = new SceneActivity($activityId);
		$arr = $obj->getSceneActivityById();
		$arr['scene_activity_arr'] = $obj->sortingData($arr['scene_activity_arr'])[0]; //根据Id取得值，所以只会有一个\
		$this->assign('act',$act);
		$this->assign('arr',$arr);
		$this->display('detail');
	}

	/**
	 * 打开添加活动页面
	 */
	public function add(){
		//分公司列表
		$bcList = $this->get_branch_company_list();
		$this->assign('bc_list',$bcList);
		$this->display();
	}

	/**
	 * 保存添加
	 */
	public function saveAdd(){
		$activityArray = [];
		$activityArray['name'] = I('request.name/s');
		$activityArray['type'] = I('request.type/d');
		$activityArray['branch_id'] = I('request.branch_id/d');
		$activityArray['rules_type'] = I('request.rules_type/d');
		$activityArray['starttime'] = I('request.starttime');
		$activityArray['endtime'] = I('request.endtime');
		$activityArray['amount'] = I('request.activity_amount/d');
		$activityArray['createtime'] = date('Y-m-d H:i:s',time());
		if($activityArray['starttime'] >= $activityArray['endtime']){

			$this->helpJsonResult(400, "活动开始时间，不能大于或者等于结束时间");
		}

		$this->validateActivityArr($activityArray); //数据验证

		$minBidPriceArr = I('request.min_bid_price');
		$maxBidPriceArr = I('request.max_bid_price');
		$amountArr = I('request.amount');

		$rulesArr = [];
		if($activityArray['rules_type'] == '2'){
			$rulesCount = count($minBidPriceArr);
			for($i = 0;$i < $rulesCount;$i++){
				$minBidPrice = $minBidPriceArr[$i];
				$maxBidPrice = $maxBidPriceArr[$i];
				$amount = $amountArr[$i];
				if(empty($minBidPrice) && empty($maxBidPrice)){ //两个价格不能都为0或者空
					$this->helpJsonResult(400, "活动规则的数据填写有误，请重新填写");
				}
				$rulesArr[$i]['min_bid_price'] = $minBidPrice;
				$rulesArr[$i]['max_bid_price'] = $maxBidPrice;
				$rulesArr[$i]['amount'] = $amount;
				//$rulesArr[$i]['rules_type'] = $activityArray['rules_type'];
				$rulesArr[$i]['emp_id'] = $this->emp_id;
				$rulesArr[$i]['createtime'] = date('Y-m-d H:i:s',time());
			}
		}

		M()->startTrans();
		$obj = new SceneActivity();
		$activityId = $obj->add($activityArray);

		foreach ($rulesArr as $key => $val){
			$rulesArr[$key]['scene_activity_id'] = $activityId;
		}

		$result = 1;
		if(!empty($rulesArr)){
			$result = $obj->addActivityRules($rulesArr);
		}
		if($activityId < 1 || $result < 1){
			M()->rollback();
			$this->helpJsonResult(400, "活动添加失败，请重新添加");
		}else{
			M()->commit();
		}

		//记录操作日志
		$log = array('emp_id' => $this->emp_id, 'order_id' => $activityId, 'action' => '活动添加成功');
		\AuctionCore\System\SystemLog::add($log);

		$this->helpJsonResult(200, "添加成功");
		return;
	}

	/**
	 *  验证活动参数
	 *
	 * @param $activityArray
	 */
	public function validateActivityArr($activityArray,$activityId = ''){

		if ($activityArray['name'] == '' || $activityArray['name'] == null) {
			$this->helpJsonResult(400, "活动名称不能为空");
		}
		if ($activityArray['type'] < 1) {
			$this->helpJsonResult(400, "请选择活动类型");
		}
		if ($activityArray['branch_id'] < 1) {
			$this->helpJsonResult(400, "请选择分公司");
		}
		if ($activityArray['starttime'] == "" || $activityArray['starttime'] == null) {
			$this->helpJsonResult(400, "请选择开始时间");
		}
		if ($activityArray['endtime'] == "" || $activityArray['endtime'] == "") {
			$this->helpJsonResult(400, "请选择结束时间");
		}
		if($activityArray['rules_type'] == '1'){
			if ($activityArray['amount'] == "" || $activityArray['amount'] == "") {
				$this->helpJsonResult(400, "金额必须大于0");
			}
		}
		$total = SceneActivity::getValidSceneActivityByName($activityArray['name'],$activityId);
		if($total > 0){
			$this->helpJsonResult(400, "该活动已经存在");
		}
	}

	/**
	 * 启用活动
	 */
	public function enableOrDisableActivity(){
		$activityId = I('request.id/d');
		$status = I('request.status/d');

		$obj = new SceneActivity($activityId);
		$result = $obj->enableOrDisableActivity($status);
		$msg = '启用场次活动成功';
		$code = '200';
		if($result === false){
			$code = '510';
			$msg = '启用场次活动失败';
		}

		//记录操作日志
		$log = array('emp_id' => $this->emp_id, 'order_id' => $activityId, 'action' => $msg);
		\AuctionCore\System\SystemLog::add($log);

		//echo M()->getLastSql();
		$this->helpJsonResult($code, $msg);
	}

	/**
	 * 禁用活动
	 */
	public function disableActivity(){
		$activityId = I('request.activity_id/d');
		$obj = new SceneActivity($activityId);
		$result = $obj->disableActivity();
		$msg = '启用场次活动成功';
		$code = '200';
		if($result === false){
			$code = '510';
			$msg = '启用场次活动失败';
		}
		//记录操作日志
		$log = array('emp_id' => $this->emp_id, 'order_id' => $activityId, 'action' => $msg);
		\AuctionCore\System\SystemLog::add($log);

		$this->helpJsonResult($code, $msg);
	}

	/**
	 * 打开编辑活动页面
	 */
	public function edite(){
		$activityId = I('request.activity_id/d');
		$obj = new SceneActivity($activityId);
		$arr = $obj->getSceneActivityById();
		$arr['scene_activity_arr'] = $obj->sortingData($arr['scene_activity_arr'])[0]; //根据Id取得值，所以只会有一个\

		//分公司列表
		$bcList = $this->get_branch_company_list();
		$this->assign('bc_list',$bcList);
		$this->assign('arr',$arr);
		$this->display();
	}

	/**
	 * 保存编辑
	 */
	public function saveEdite(){
		$activityArray = [];
		$activityId = I('request.activity_id/d');
		$activityArray['name'] = I('request.name/s');
		$activityArray['type'] = I('request.type/d');
		$activityArray['branch_id'] = I('request.branch_id/d');
		$activityArray['rules_type'] = I('request.rules_type/d');
		$activityArray['starttime'] = I('request.starttime');
		$activityArray['endtime'] = I('request.endtime');
		$activityArray['amount'] = I('request.activity_amount/d');
		if($activityArray['starttime'] > $activityArray['endtime']){

			$this->helpJsonResult(400, "活动开始时间，不能大于结束时间");
		}
		$this->validateActivityArr($activityArray,$activityId); //数据验证

		$minBidPriceArr = I('request.min_bid_price');
		$maxBidPriceArr = I('request.max_bid_price');
		$amountArr = I('request.amount');

		$rulesArr = [];
		if($activityArray['rules_type'] == '2'){
			$rulesCount = count($minBidPriceArr);
			for($i = 0;$i < $rulesCount;$i++){
				$minBidPrice = $minBidPriceArr[$i];
				$maxBidPrice = $maxBidPriceArr[$i];
				$amount = $amountArr[$i];
				if(empty($minBidPrice) && empty($maxBidPrice)){ //两个价格不能都为0或者空
					$this->helpJsonResult(400, "活动规则的数据填写有误，请重新填写");
				}
				$rulesArr[$i]['scene_activity_id'] = $activityId;
				$rulesArr[$i]['min_bid_price'] = $minBidPrice;
				$rulesArr[$i]['max_bid_price'] = $maxBidPrice;
				$rulesArr[$i]['amount'] = $amount;
				$rulesArr[$i]['rules_type'] = $activityArray['rules_type'];
				$rulesArr[$i]['emp_id'] = $this->emp_id;
				$rulesArr[$i]['createtime'] = date('Y-m-d H:i:s',time());
			}
		}

		$obj = new SceneActivity($activityId);
		$result1 = $obj->edite($activityArray);
		$result2 = $obj->deleteRulesByActivityId(); //删除相应的规则

		$result3 = true;
		if(!empty($rulesArr)){
			$result3 = $obj->addActivityRules($rulesArr);
		}
		if($result1 === false || $result2 === false || $result3 === false){
			M()->rollback();
			$this->helpJsonResult(400, "活动更新失败，请重新编辑");
		}else{
			M()->commit();
		}

		//记录操作日志
		$log = array('emp_id' => $this->emp_id, 'order_id' => $activityId, 'action' => '活动编辑成功');
		\AuctionCore\System\SystemLog::add($log);

		$this->helpJsonResult(200, "编辑成功");
		return;
	}

	/**
	 * 根据规则编号，删除规则
	 **/
	public function deleteRulesById(){
		$id = I('request.id/d');
		$obj = new SceneActivity();
		$result = $obj->deleteActivityRulesById($id);

		if($result === false){
			$this->helpJsonResult(400, "删除失败，请重新操作");
		}

		//记录操作日志
		$log = array('emp_id' => $this->emp_id, 'order_id' => $id, 'action' => '活动规则删除成功');
		\AuctionCore\System\SystemLog::add($log);

		$this->helpJsonResult(200, "删除成功");
		return;
	}


}